// pages/components/Share/share.js
import {request,canDownload} from '../../../utils/util'
const { $Message } = require('../../../dist/base/index');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    name: "",
    type: "",
    uuid:"",
    url:"",
    switch1: true,
    isLost:false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const that = this;
    const isLost = options.isLost || false
    const name = options.name;
    const type = options.type;
    const uuid = options.uuid;
    this.setData({
      name: name,
      type: type,
      uuid:uuid,
      isLost:isLost
    })
    // const param = {
    //   file_hash:uuid
    // }
    // request("POST","/api/upload/doc/preview/",param)
    // .then(res =>{
    //   that.setData({
    //     name: name,
    //     type: type,
    //     uuid:uuid,
    //     url:res.data.url
    //   })
    // })
    
  },



  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    const userId = wx.getStorageSync('userInfo').id
      return{
        title:`${this.data.name}`,
        path:`/pages/webView/previewShare/previewShow?uuid=${this.data.uuid}&userId=${userId}&name=${this.data.name}&type=${this.data.type}`
      }
  },
  onChange(event) {
    const detail = event.detail;
    this.setData({
      'switch1': detail.value
    })
  },
  copy(){
    wx.setClipboardData({
      data: this.data.url,
      success (res) {
        wx.showToast({
          title: '复制到粘贴版成功',
          icon:"success"
        })
      }
    })
  },
  fileShare(){
    const isAllowDownload = canDownload(this.data.type)
    const fileName = this.data.name
    const allowImage = ["png","jeg","jpeg"]
    const type = this.data.type
    if (isAllowDownload) {
      const param = {
        file_hash: this.data.uuid,
        type:'download'
      }
      request("POST", "/api/upload/doc/download/", param)
        .then(res => {
          if (res.statusCode === 200) {
            $Message({
              content: '开始下载',
            })
            const downloadTask = wx.downloadFile({
              url: res.data.url, 
              filePath:wx.env.USER_DATA_PATH + "/" + fileName,
              success(res) {
               
                if(allowImage.indexOf(type) !== -1){
                  wx.previewImage({
                    urls:[res.filePath],
                    showMenu:true,
                    success: function (res) {
                      console.log('打开文档成功')
                    },
                    fail: res => {
                      console.log(res)
                    }
                  })
                }else{
                  wx.openDocument({
                    filePath: res.filePath,
                    showMenu: true,
                    fileType: type,
                    success: function (res) {
                      console.log('打开文档成功')
                    },
                    fail: res => {
                      console.log(res)
                    }
                  })
                }
                

              },
              fail: res => {
                console.log(res)
              }

            })
            downloadTask.onProgressUpdate((res) => {
         
              $Message({
                content: '正在下载' + res.progress + '%',
              })
              if (res.progress === 100) {
                $Message({
                  content: '下载成功,正在打开',
                  type: 'success',

                })
              }
            })

          } else if (res.statusCode === 430) {
            wx.showToast({
              title: res.data.detail,
              icon: "none"
            })
          } else {
            wx.showToast({
              title: '服务器异常，请联系系统管理员',
              icon: 'none'
            })
          }
        })
    } else {
      wx.showToast({
        title: '该文件类型暂不支持源文件分享',
        icon: 'none',
        duration: 1500
      })
    }
  }
  
})